package ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import midlet.VadeMecumMIDlet;
import multimedia.HTTPConnectionThread;

public class JosekiQueryForm extends Form implements CommandListener {

	private VadeMecumMIDlet midlet;

	private Displayable previous;

	private Command queryCommand = null;

	private Command back = null;

	private TextField query = null;

	private String endl = "\n";

	public JosekiQueryForm(VadeMecumMIDlet midlet, Displayable previous) {
		super("Create a text note");

		this.midlet = midlet;
		this.previous = previous;

		queryCommand = new Command("Query", Command.OK, 1);
		back = new Command("BACK", Command.BACK, 1);
		addCommand(queryCommand);
		addCommand(back);

		query = new TextField("", "", 500, TextField.ANY);
		query.setString("PREFIX+vade%3A+%3Chttp%3A%2F%2Fwww.lsi.dsc.ufcg.edu.br%2Fvademecum.owl%23%3E%0D%0APREFIX+dc%3A++++++%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Felements%2F1.1%2F%3E%0D%0APREFIX+rdfs%3A++++%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+rdf%3A++++++%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0D%0ASELECT+%3Fc%0D%0AWHERE+%0D%0A++{+%0D%0A++++vade%3AHugo+vade%3AhasContext+%3Fc+.%0D%0A++}");

		/* "PREFIX vade: <http://www.lsi.dsc.ufcg.edu.br/vademecum.owl#>"+// endl +
						"PREFIX dc:      <http://purl.org/dc/elements/1.1/>" +// endl +
						"PREFIX rdfs:       <http://www.w3.org/2000/01/rdf-schema#>" +// endl +
						"PREFIX rdf:      <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +// endl +
						"SELECT ?b" +// endl +
						"WHERE" +// endl +
						"{"+
							"vade:Hugo vade:hasContext ?c ." +// endl +
							"?c rdf:type vade:Emotion ." +// endl +
							"?c vade:emotionValue ?b" +// endl +
						"}"); */
		append(query);
		setCommandListener(this);
	}

	public void commandAction(Command cmd, Displayable disp) {
		if (this.queryCommand == cmd) {
			doQuery();
		} else if(this.back == cmd) {
			midlet.display.setCurrent(this.previous);
		}		
	}

	public void doQuery() {
		Thread queryThread = new Thread(new HTTPConnectionThread(this.midlet, this.previous, query.getString()));
		queryThread.start();
	}

}
